package com.bwie.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.dto.TypeDto;
import com.bwie.poi.StringUtils;
import com.bwie.utils.JwtsUtils;
import com.bwie.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TypeMapper;
import com.bwie.domain.Type;
import com.bwie.service.TypeService;

import javax.servlet.http.HttpServletRequest;

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService{

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;


    @Override
    public R saveType(TypeDto typeDto) {

        //初始化审计字段
        String token = request.getHeader("token");
        Integer idByToken = JwtsUtils.getIdByToken(token);
        Type type = new Type();
        type.setCreateBy(idByToken);
        type.setCreateDate(new Date());
        type.setDeleteFlag(0);
        //如果父级ID 为空，则是顶级分类
        if (typeDto.getTypePid() == null){
            typeDto.setTypePid(0);
        }
        //参数传递
        BeanUtils.copyProperties(typeDto,type);
        //调用保存方法
        typeMapper.insert(type);
        //返回前端
        return R.OK();
    }

    @Override
    public R selectTypeTree(TypeDto typeDto) {
        //查询未删除的列表信息
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(typeDto.getTypeName())){
            wrapper.like(Type::getTypeName,typeDto.getTypeName());
        }
        if (ObjectUtils.isNotEmpty(typeDto.getTypeStatus())){
            wrapper.eq(Type::getTypeStatus,typeDto.getTypeStatus());
        }
        wrapper.eq(Type::getDeleteFlag,0);
        List<Type> typeList = typeMapper.selectList(wrapper);

        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (type, TreeNode) -> {
            TreeNode.setParentId(type.getTypePid().toString());
            TreeNode.setId(type.getTypeId().toString());
            TreeNode.putExtra("typeName",type.getTypeName());
            TreeNode.putExtra("typeImage",type.getTypeImage());
            TreeNode.putExtra("typeTop",type.getTypeTop());
            TreeNode.putExtra("typeStatus",type.getTypeStatus());
            TreeNode.putExtra("value",type.getTypeId().toString());
            TreeNode.putExtra("label",type.getTypeName());
        });


        return R.OK(treeList);
    }

    @Override
    public R getTypeTree() {
        List<Type> typeList = typeMapper.selectList(null);
        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (type, TreeNode) -> {
            TreeNode.setParentId(type.getTypePid().toString());
            TreeNode.setId(type.getTypeId().toString());
            TreeNode.putExtra("typeName",type.getTypeName());
            TreeNode.putExtra("typeImage",type.getTypeImage());
            TreeNode.putExtra("typeTop",type.getTypeTop());
            TreeNode.putExtra("typeStatus",type.getTypeStatus());
            TreeNode.putExtra("value",type.getTypeId().toString());
            TreeNode.putExtra("label",type.getTypeName());
        });
        return R.OK(treeList);
    }
}
